package org.starsource.forum.client.view;

import java.util.ArrayList;
import java.util.List;

import org.starsource.forum.client.StarForum;
import org.starsource.forum.client.StarForum.AppWindow;
import org.starsource.forum.client.domain.ResourceObject;
import org.starsource.forum.client.util.ImageRatioHelper;
import org.starsource.forum.client.widget.handler.ImageClickHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class ImageBrowser extends Composite {

	interface Binder extends UiBinder<Widget, ImageBrowser> { }

	private static final Binder binder = GWT.create(Binder.class);
	public interface ImageBrowserImages extends ClientBundle {
		@Source(value = { "left.png" })
	    ImageResource left();

		@Source(value = { "right.png" })
	    ImageResource right();

		@Source(value = { "close.png" })
	    ImageResource close();

		@Source(value = { "play.png" })
	    ImageResource play();

		@Source(value = { "pause.png" })
	    ImageResource pause();
	}

	private ImageBrowserImages buttons = (ImageBrowserImages) GWT.create(ImageBrowserImages.class);

	@UiField SplitLayoutPanel mainPanel;
	@UiField Image closeButton;
	@UiField Image leftButton;
	@UiField Image rightButton;
//	@UiField Image playButton;
	@UiField Image focusImage;
	@UiField ImageSlider imageSlider;
	@UiField FocusPanel focusPanel;
	@UiField Anchor link;

	private ImageClickHandler imageClickHandler = new ImageClickHandler() {

		@Override
		public void onImageClick(String url, int width, int height) {
			for(int index = 0; index < imageList.size(); index ++) {
				if (url.equals("filedownload?resourceId=" +  imageList.get(index).getId() + "&thumbnail=false")) {
					 imageSlider.setFocus(index);
					 link.setText(imageList.get(index).getUrl());
					 link.setHref("filedownload?resourceId=" +  imageList.get(index).getId() + "&thumbnail=false");
				}
			}
			focusImage(url, width, height);
		}

	};

	private HandlerRegistration focusImageHandler;
	private void focusImage(final String url, final int width, final int height) {
		focusImage.setUrl(url);
		if (focusImageHandler != null) {
			focusImageHandler.removeHandler();
		}
		focusImageHandler = focusImage.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				ImageRatioHelper ratio = new ImageRatioHelper(width, height,focusPanel.getOffsetWidth(), focusPanel.getOffsetHeight());
				ratio.calculateScale();
				focusImage.setWidth(ratio.getScaleWidth() + "px");
				focusImage.setHeight(ratio.getScaleHeight() + "px");
			}

		});


		//		Window.alert("IMAGE("+ ratio.getImageWidth() + ","+ ratio.getImageHeight()+ ") CONTAINER("+ratio.getContainerWidth()
//				+ ","+ ratio.getContainerHeight() + ") SCALE("+ ratio.getScaleWidth() + ","+ ratio.getScaleHeight()+ ")");
	}

	public ImageBrowser() {
		initWidget(binder.createAndBindUi(this));

		closeButton.setResource(buttons.close());
		leftButton.setResource(buttons.left());
		rightButton.setResource(buttons.right());
//		playButton.setResource(buttons.play());

		mainPanel.setStylePrimaryName("gwter-SplitLayoutPanel");
		mainPanel.setWidth(Window.getClientWidth() + "px");
		mainPanel.setHeight(Window.getClientHeight()  + "px");
//		mainPanel.setStylePrimaryName("SplitLayoutPanel-Black");
		leftButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				imageSlider.backward();
			}

		});
		rightButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				imageSlider.forward();
			}

		});

		imageSlider.setImageClickHandler(imageClickHandler);
		leftButton.setStylePrimaryName("clickableImage");
		rightButton.setStylePrimaryName("clickableImage");
		closeButton.setStylePrimaryName("clickableImage");
		focusPanel.addMouseMoveHandler(new MouseMoveHandler() {

			@Override
			public void onMouseMove(MouseMoveEvent event) {
				if (event.getClientX() <= (focusImage.getAbsoluteLeft() + focusImage.getWidth() / 2)) {
					focusPanel.removeStyleName("rightArrow");
					focusPanel.addStyleName("leftArrow");
				} else {
					focusPanel.removeStyleName("leftArrow");
					focusPanel.addStyleName("rightArrow");
				}
			}

		});
		focusPanel.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (event.getNativeEvent().getClientX() <= (focusImage.getAbsoluteLeft() + focusImage.getWidth() / 2)) {
					imageSlider.backward();
				} else {
					imageSlider.forward();
				}
			}

		});
		link.setStylePrimaryName("anchor");
	}
	private List<ResourceObject> imageList = new ArrayList<ResourceObject>();

	public void setImageList(List<ResourceObject> imageList) {
		this.imageList = imageList;
		imageSlider.setImageList(imageList);

		if (imageList.size() > 0) {
			link.setText(imageList.get(0).getUrl());
			link.setHref("filedownload?resourceId=" +  imageList.get(0).getId() + "&thumbnail=false");
		}

//		try {
//			focusImage(new Image("filedownload?resourceId=" +  imageList.get(0).getId()));
//		} catch (Exception e) {
//
//		}
	}

	@UiHandler("closeButton")
	public void onCloseButtonClicked(ClickEvent event) {
		StarForum.getInstance().switchWindow(AppWindow.DESKTOP);
	}

}
